
fn main() {
    // 1. 定义结构体
    #[derive(Debug)]
    struct User {
        name: String,
        count: String,
        nonce: u64,
        active: bool,
    }

    // 2. 创建结构体实例
    let xiaoming = User {
        name: String::from("xiaoming"),
        count: String::from("800800"),
        nonce: 1000000,
        active: true,
    };

    // 3. 修改结构体字段
    let mut xiaohong = User { // 修改需要加上mut关键字哈，这里一定一定要记住
        name: String::from("xiaohong"),
        count: String::from("800800"),
        nonce: 1000000,
        active: true,
    };
    xiaohong.nonce = 2000000;

    // 4. 参数名和字段名同名时的简单写法
    let name = String::from("xiaoxiao");
    let count = String::from("800900");
    let nonce = 5000000;
    let active = false;

    // let user1 = User {
    //     name: name,
    //     count: count,
    //     nonce: nonce,
    //     active: active,
    // };
    // 新的写法
    let user = User{
        name,
        count,
        nonce,
        active,
    };

    // 5. 从其他结构体创建实例
    let user2 = User {
        name: String::from("Max"),
        ..user
    };
    println!("name = {}", user2.name);

    // 6. 元组结构体: 字段没有名字、必须是圆括号
    struct Point(i32, i32);
    let a = Point(10, 20);
    let b = Point(30, 40);
    println!("a.x = {}, a.y = {}", a.0, a.1);
    println!("b.x = {}, b.y = {}", b.0, b.1);

    // 7. 没有任何字段的类单元结构体
    struct _A{};

    // 8. 打印结构体
    println!("xiaoming = {:#?}", xiaoming);
    println!("xiaohong = {:#?}", xiaohong);
}
